package org.come.Jpanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import org.come.Frame.EverydayRechargeJframe;
import org.come.bean.ChongjipackBean;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.FormsManagement;
import org.come.until.ScrollUI;
import org.come.until.SrcollPanelUI;

import com.tool.btn.FormsOnOffBtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;



/**
 * 每日充值
 * 
 * @author Administrator
 * 
 */
public class EverydayRechargeJpanel extends JPanel {

	private JList<EverydayRechargeGoodsJpanel> goodsJpanels;
	private JScrollPane paneList;

	private List<ChongjipackBean> chongjipackBeans;

	public EverydayRechargeJpanel() {
		setPreferredSize(new Dimension(682, 404));
		setOpaque(false);
		setLayout(null);

		// 关闭按钮事件
		FormsOnOffBtn offBtn = new FormsOnOffBtn("skin/8100226/newbutton/cjlbgb.png", 1, 100);
		offBtn.setBounds(652, 0, 30, 30);
		this.add(offBtn);

		getPaneList();
	}

	/**
	 * 获取每日充值列表
	 */
	public void getGoods() {
		String sendmes = Agreement.getAgreement().PaydaygradepayAgreement();
		SendMessageUntil.toServer(sendmes);
	}

	/**
	 * 展示每日充值列表
	 * 
	 * @param chongjipackBeans
	 */
	public void showGoods(List<ChongjipackBean> chongjipackBeans) {
		if (chongjipackBeans == null) {
			chongjipackBeans = new ArrayList<>();
		}
		this.chongjipackBeans = chongjipackBeans;
		String vipget = RoleData.getRoleData().getLoginResult().getVipget();
		String[] split = null;
		if (vipget != null) {
			split = vipget.split("&&");
		}
		int componentCount = goodsJpanels.getComponentCount();
		for (int i = 0; i < chongjipackBeans.size() - 1; i++) {
			for (int j = i + 1; j < chongjipackBeans.size(); j++) {
				ChongjipackBean chongjipackBeani = chongjipackBeans.get(i);
				ChongjipackBean chongjipackBeanj = chongjipackBeans.get(j);
				if (chongjipackBeani.getCanpaymoney() > chongjipackBeanj.getCanpaymoney()) {
					chongjipackBeans.set(i, chongjipackBeanj);
					chongjipackBeans.set(j, chongjipackBeani);
				}
			}
		}
		for (int i = 0, size = chongjipackBeans.size(); i < size; i++) {
			if (componentCount < chongjipackBeans.size()) {
				EverydayRechargeGoodsJpanel everydayRechargeGoodsJpanel = new EverydayRechargeGoodsJpanel();
				everydayRechargeGoodsJpanel.setBounds(0, componentCount * 60, 562, 60);
				goodsJpanels.add(everydayRechargeGoodsJpanel);
				componentCount++;

			}
			EverydayRechargeGoodsJpanel goodsJpanel = (EverydayRechargeGoodsJpanel) goodsJpanels.getComponent(i);
			goodsJpanel.showGoods(chongjipackBeans.get(i));
			if (ImpactGradeGoodsJpanel.checkYesOrNo(split, "2", chongjipackBeans.get(i).getPackgradetype() + "")) {
				goodsJpanel.getGetBtn().setBtn(-1);
				goodsJpanel.getGetBtn().setIcon(CutButtonImage.getImage("inkImg/hongmu/1/yilingqu.png", -1, -1));
			} else {
				goodsJpanel.getGetBtn().setBtn(1);
				try {
					goodsJpanel.getGetBtn().setIcons(CutButtonImage.cuts("inkImg/hongmu/1/lingqu.png"));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}
		goodsJpanels.setPreferredSize(new Dimension(562, 60 * chongjipackBeans.size()));

		if (!EverydayRechargeJframe.getEverydayRechargeJframe().isVisible()) {
			FormsManagement.showForm(100);
		}
	}

	/**
	 * 刷新面板
	 */
	public void refreshPanel() {
		showGoods(chongjipackBeans);
	}

	private ImageIcon iconBack;
	private ImageIcon iconTop;

	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);

		if (iconBack == null) {
			iconBack = CutButtonImage.getImage("inkImg/old/background/S8.png", -1, -1);
		}
		// 底板_w682,h404
		g.drawImage(iconBack.getImage(), 0, 0, 682, 404, this);

		if (iconTop == null) {
			iconTop = CutButtonImage.getImage("inkImg/old/background/S4.png", -1, -1);
		}
		g.drawImage(iconTop.getImage(), 52, 30, 591, 116, this);//每日充值图片替换
	}

	public JList<EverydayRechargeGoodsJpanel> getGoodsJpanels() {
		if (goodsJpanels == null) {
			goodsJpanels = new JList<EverydayRechargeGoodsJpanel>();
			goodsJpanels.setSelectionBackground(new Color(122, 117, 112));
			goodsJpanels.setSelectionForeground(Color.white);
			goodsJpanels.setForeground(Color.white);
			goodsJpanels.setFont(UIUtils.TEXT_HY16);
			goodsJpanels.removeAll();
			goodsJpanels.setBackground(UIUtils.Color_BACK); // 设置列表框为透明背景
			DefaultListCellRenderer cellRenderer = new DefaultListCellRenderer() {
				@Override
				public Component getListCellRendererComponent(javax.swing.JList<?> list, Object value, int index,
						boolean isSelected, boolean cellHasFocus) {
					super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
					return this;
				}
			};
			goodsJpanels.setCellRenderer(cellRenderer);
			goodsJpanels.setOpaque(false);
		}
		return goodsJpanels;
	}

	public void setGoodsJpanels(JList<EverydayRechargeGoodsJpanel> goodsJpanels) {
		this.goodsJpanels = goodsJpanels;
	}

	public ImageIcon getIconBack() {
		return iconBack;
	}

	public void setIconBack(ImageIcon iconBack) {
		this.iconBack = iconBack;
	}

	public ImageIcon getIconTop() {
		return iconTop;
	}

	public void setIconTop(ImageIcon iconTop) {
		this.iconTop = iconTop;
	}

	public JScrollPane getPaneList() {
		if (paneList == null) {
			paneList = new JScrollPane(getGoodsJpanels());
			paneList.setVerticalScrollBarPolicy(22);
			paneList.getVerticalScrollBar().setUI(new ScrollUI());
			paneList.getVerticalScrollBar().setUnitIncrement(20);
			paneList.getViewport().setOpaque(false);
			paneList.setOpaque(false);
			paneList.setBounds(54, 174, 592, 207);
			paneList.setBorder(BorderFactory.createEmptyBorder());
			paneList.setHorizontalScrollBarPolicy(31);
			this.add(paneList);
		}

		return paneList;
	}

	public void setPaneList(JScrollPane paneList) {
		this.paneList = paneList;
	}

}
